from app.accounts.models import UserWorkerInfo, User
from rest_framework.views import APIView
from app.accounts.serializers import AddUserWorkerInfoSerializers
from rest_framework.response import Response


class UserWorkerInfoView(APIView):

    def post(self, request):
        serializer = AddUserWorkerInfoSerializers(data=request.data)
        serializer.is_valid(raise_exception=True)

        user, created = User.objects.get_or_create(username=serializer.data["openid"], openid=serializer.data["openid"])
        user.openid = serializer.data["openid"]
        user.username = serializer.data["openid"]
        user.first_name = serializer.data["name"]
        user.sex = serializer.data["sex"]
        user.phone = serializer.data["phone"]
        user.email = serializer.data["email"]
        user.save()

        UserWorkerInfo.objects.update_or_create(
            user=user,
            defaults={
                "education": serializer.data["education"],
                "work_start_date": serializer.data["work_start_date"],
                "work_end_date": serializer.data["work_end_date"],
                "work_type_list": serializer.data["work_type_list"],
                "work_position_list": serializer.data["other_work_position"],
                "other_work_type": serializer.data["other_work_type"],
                "other_work_position": serializer.data["other_work_position"],
            },
        )

        return Response({"message": "添加成功"})
